package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Franchiser;
import com.ruoyi.system.domain.PlayerCostLog;
import com.ruoyi.system.mapper.PlayerCostLogMapper;
import com.ruoyi.system.service.IFranchiserService;
import com.ruoyi.system.service.IPlayerCostLogService;
import com.ruoyi.web.controller.domain.PlayerCostLogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 玩家抽卡消耗Controller
 *
 * @author ruoyi
 * @date 2024-07-19
 */
@RestController
@RequestMapping("/system/log")
public class PlayerCostLogController extends BaseController {
    @Autowired
    private IPlayerCostLogService playerCostLogService;
    @Autowired
    private PlayerCostLogMapper playerCostLogMapper;

    @Autowired
    private IFranchiserService franchiserService;

    /**
     * 分销商-查询玩家抽卡消耗记录
     */
    @PreAuthorize("@ss.hasRole('custom_seller')")
//    @PreAuthorize("@ss.hasPermi('system:log:list')")
    @GetMapping("/list")
    public TableDataInfo list(PlayerCostLogQuery query) {
        startPage();
        String username = SecurityUtils.getUsername();
        // TODO 这里可以保存到 sysuser数据中
        Franchiser franchiser = new Franchiser();
        franchiser.setPhone(username);
        franchiser.setPrimaryId(0L);
        List<Franchiser> franchiserList = franchiserService.selectFranchiserList(franchiser);
        franchiser = franchiserList.get(0);

        PlayerCostLog log = new PlayerCostLog();
        log.setPlayerId(query.getPlayerId());
        log.setFranchiserId(franchiser.getFranchiserId());
        List<PlayerCostLog> list = playerCostLogService.selectPlayerCostLogList(log);
        List<PlayerCostLog> selects = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (query.getTime() != null) {
                if (list.get(i).getTime().getTime() < query.getTime().getTime()) {
                    continue;
                }
            }
            if (query.getEndTime() != null) {
                if (list.get(i).getTime().getTime() > query.getEndTime().getTime() + 86400_000L) {
                    continue;
                }
            }
            selects.add(list.get(i));
        }
        return getDataTable(selects);
    }

    /**
     * 导出玩家抽卡消耗列表
     */
    @PreAuthorize("@ss.hasPermi('system:log:export')")
    @Log(title = "玩家抽卡消耗", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlayerCostLog playerCostLog) {
        List<PlayerCostLog> list = playerCostLogService.selectPlayerCostLogList(playerCostLog);
        ExcelUtil<PlayerCostLog> util = new ExcelUtil<PlayerCostLog>(PlayerCostLog.class);
        util.exportExcel(response, list, "玩家抽卡消耗数据");
    }

    /**
     * 获取玩家抽卡消耗详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:log:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(playerCostLogService.selectPlayerCostLogById(id));
    }

    /**
     * 新增玩家抽卡消耗
     */
    @PreAuthorize("@ss.hasPermi('system:log:add')")
    @Log(title = "玩家抽卡消耗", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlayerCostLog playerCostLog) {
        return toAjax(playerCostLogService.insertPlayerCostLog(playerCostLog));
    }

    /**
     * 修改玩家抽卡消耗
     */
    @PreAuthorize("@ss.hasPermi('system:log:edit')")
    @Log(title = "玩家抽卡消耗", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlayerCostLog playerCostLog) {
        return toAjax(playerCostLogService.updatePlayerCostLog(playerCostLog));
    }

    /**
     * 删除玩家抽卡消耗
     */
    @PreAuthorize("@ss.hasPermi('system:log:remove')")
    @Log(title = "玩家抽卡消耗", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(playerCostLogService.deletePlayerCostLogByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('system:log:user')")
    @GetMapping("/franchiser_id")
    public AjaxResult franchiserId() {
        return success();
    }
}
